<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form', '', 0);
/**
 * 栏目绑定资源
 */
class resources extends admin {
	private $db,$type;
	public $siteid;
	function __construct() {
        $this->news = pc_base::load_model('news_nsr_model');
        $this->picture = pc_base::load_model('picture_nsr_model');
        $this->video = pc_base::load_model('video_nsr_model');
		$this->db = pc_base::load_model('category_source_model');
        $this->type = pc_base::load_model('source_type_model');
		$this->siteid = get_siteid();
        $model_setting=pc_base::load_model('site_setting_model');
        $this->setting = $model_setting->getValue("sname_link",$this->siteid);
        $this->t_type = $model_setting->getValue("talent_type",$this->siteid);//获取人才类型
        $this->author_title = $model_setting->getValue("author_title",$this->siteid);
		parent::__construct();
	}

	/**
	 * xinsilu/v1/refreshRedis/refreshRedis?type=9:刷新小游戏缓存的
        类型(type值)：1-栏目和栏目关联资源,2-投票和订单,3-轮播,4-报名,5-证书,6-人才和人才关联资源,7-投票排行,8-动态,9-眼技大考验小游戏
        type传参不同刷新不同
        1和6都是会刷新资讯图集和视频
        
        
        * Parameter: name
     *Description :变量-VARIABLE:*;认证审核-authentication:authenticationhash:createBy[createdBy]
     *2017-11-22 15:42:43
     *angel_in_us
	 * */
    public function ajax_data ()
    {
        $name = trim($_GET['name']);
        $type = (int) $_GET['type'];
        $catid = (int) $_GET['catid'];
        if($name){
            $data = file_get_contents(API_URL.'/xinsilu/v1/refreshRedis/deleteRedis?name='.$name);
            echo $data;die;
        }
        if ($catid) {
            $data = file_get_contents(API_URL.'/xinsilu/v1/refreshRedis/refreshCategory?ids='.$catid);
        } else {
            $data = file_get_contents(API_URL.'/xinsilu/v1/refreshRedis/refreshRedis?type='.$type);
        }
        echo $data;die;
    }

    /**
     * 全部资源列表
     */
    public function init () {
        if(isset($_GET['catid']) && $_GET['catid']){
            $show_dialog = $show_validator = true;
            $catid = (int) $_GET['catid'];
            $category_model = pc_base::load_model('category_model');
            $data = $category_model->get_one(array('catid' => $catid));
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
            $where = ' cate_id = ' . $catid;
            $list = $this->db->listinfo($where, 'sort DESC,update_time DESC,source_id DESC', $page, 10);
            foreach ($list as $k => $v) {
                if ($v['source_type'] == 1) {
                    $info = $this->news->get_one(' id = '.$v['source_id'].' and status <> 2 ');
                    if ($info) {
                        $list[$k]['title'] = $info['title'];
                        $list[$k]['image_cover'] = $v['image']?$v['image']:$info['image_cover'];
                        $list[$k]['publish_time'] = $info['publish_time']>0?$info['publish_time']:$info['create_time'];
                        $list[$k]['typeName'] = '资讯';
                    } else {
                        unset($list[$k]);
                    }
                } else if ($v['source_type'] == 2) {
                    $info = $this->video->get_one(array('id'=>$v['source_id'],'status'=>1));
                    if ($info) {
                        $list[$k]['title'] = $info['title'];
                        $list[$k]['image_cover'] = $v['image']?$v['image']:$info['image_cover'];
                        $list[$k]['publish_time'] = $info['create_time'];
                        $list[$k]['typeName'] = '视频';
                    } else {
                        unset($list[$k]);
                    }
                } else if ($v['source_type'] == 3) {
                    $info = $this->picture->get_one(' id = '.$v['source_id'].' and status <> 2 ');
                    if ($info) {
                        $list[$k]['title'] = $info['title'];
                        $list[$k]['image_cover'] = $v['image']?$v['image']:$info['image_cover'];
                        $list[$k]['publish_time'] = $info['create_time'];
                        $list[$k]['typeName'] = '图集';
                    } else {
                        unset($list[$k]);
                    }
                } else if ($v['source_type'] == 4) {
                    $talent_model = pc_base::load_model('talent_model');
                    $info = $talent_model->get_one(array('id'=>$v['source_id'],'status'=>1));
                    if ($info) {
                        $list[$k]['title'] = $info['real_name'];
                        $list[$k]['image_cover'] = $v['image']?$v['image']:$info['image_square'];
                        $list[$k]['publish_time'] = $info['create_time'];
                        $list[$k]['typeName'] = '人才';
                    } else {
                        unset($list[$k]);
                    }
                } else if ($v['source_type'] == 5) {
                    $contest_model = pc_base::load_model('contest_model');
                    $info = $contest_model->get_one(array('id'=>$v['source_id'],'status'=>1));
                    if ($info) {
                        $list[$k]['title'] = $info['name'];
                        $list[$k]['image_cover'] = $v['image']?$v['image']:$info['image_square'];
                        $list[$k]['publish_time'] = $info['create_time'];
                        $list[$k]['typeName'] = '赛事';
                    } else {
                        unset($list[$k]);
                    }
                } else if ($v['source_type'] == 6) {
                    $course_nsr_model = pc_base::load_model('course_nsr_model');
                    $info = $course_nsr_model->get_one(array('id'=>$v['source_id'],'status'=>1));
                    if ($info) {
                        $list[$k]['title'] = $info['name'];
                        $list[$k]['image_cover'] = $v['image']?$v['image']:$info['image_square'];
                        $list[$k]['publish_time'] = $info['create_time'];
                        $list[$k]['typeName'] = '课程';
                    } else {
                        unset($list[$k]);
                    }
                }
            }
            $pages = $this->db->pages;
            pc_base::load_sys_class('format', '', 0);
            include $this->admin_tpl('lists');
        } else {
           include $this->admin_tpl('content_quick'); 
        }
    }
	
	/**
	 * 资源列表
	 */
	function index () {
        //获取管理员真实姓名数据
        $admin_model = pc_base::load_model('admin_model');
        $adminInfo = $admin_model->getRealnameInfo();
		$source_type = (int) $_GET['source_type'];
		$catid = (int) $_GET['catid'];
		if($source_type==1){
            $table="nsrweb_news_nsr";
            $btn_info="资讯";
        }elseif($source_type==2){
            $table="nsrweb_video_nsr";
            $btn_info="视频";
        }elseif($source_type==3){
            $table="nsrweb_picture_nsr";
            $btn_info="图集";
        }

        $category_model = pc_base::load_model('category_model'); // 加载model
        $data = $category_model->get_one(array('catid' => $catid));
        $show_dialog = $show_validator = true;
        $sql="SELECT n.*,c.id as cid,c.is_top,c.sort,c.updated_by as binduser,c.image  FROM nsrweb_category_source AS c LEFT JOIN ".$table." AS n ON c.source_id = n.id WHERE n.status <> 2";

        // 搜索框
        $title = isset($_GET['title']) ? $_GET['title'] : '';
        $typeid = isset($_GET['typeid']) ? $_GET['typeid'] : '';

        if (isset($_GET['search'])) {
            if ($title) {
                $sql .= " and n.`title` LIKE '%$title%'";
            }

            if ($typeid) {
                $sql .= " and n.type_id = {$typeid}";
            }
        }
        $select_categorys = $this->type->getSource($source_type,$typeid);
        $sql.=" AND c.cate_id=".$catid." AND c.source_type = ".$source_type." AND n.siteid = ".$this->siteid." ORDER BY c.sort DESC,c.update_time DESC,n.id DESC";

        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $sourceinfo = $this->db->my_listinfo($sql, $page, 10);
        $pages = $this->db->pages;

        include $this->admin_tpl('resources_list');
	}

    /**
     * 栏目绑定课程列表
     */
    public function cIndex ()
    {
        $types = $this->t_type;
        $catid = (int) $_GET['catid'];
        $category_model = pc_base::load_model('category_model'); // 加载model
        $data = $category_model->get_one(array('catid' => $catid));
        $sql="select n.*,t.id as cid,t.is_top,t.sort,t.image from nsrweb_category_source as t LEFT JOIN nsrweb_course_nsr as n on t.source_id =n.id where n.status=1 ";
        $sql .= " and t.source_type = 6 and  t.cate_id=".$catid." and siteid =" . $this->siteid;
        if (isset($_GET['name']) && !empty($_GET['name'])) {
            $name = trim($_GET['name']);
            $sql .= " and `name` like '%$name%' ";
        }

        $sql .= " order by t.sort DESC,t.update_time DESC,n.id DESC ";
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $talentinfo = $this->db->my_listinfo($sql, $page, 10);

        $pages = $this->db->pages;
        include $this->admin_tpl('course_list');
    }

    /**
     * 栏目绑定人才列表
     */
    public function tIndex ()
    {
        $types = $this->t_type;
        $catid = (int) $_GET['catid'];
        $category_model = pc_base::load_model('category_model'); // 加载model
        $data = $category_model->get_one(array('catid' => $catid));
        $sql="select n.*,t.id as cid,t.is_top,t.sort,t.image from nsrweb_category_source as t LEFT JOIN nsrweb_talent as n on t.source_id =n.id where n.status=1 ";
        $sql .= " and t.source_type = 4 and  t.cate_id=".$catid." and siteid =" . $this->siteid;
        if (isset($_GET['name']) && !empty($_GET['name'])) {
            $name = trim($_GET['name']);
            $sql .= " and `real_name` like '%$name%' ";
        }

        if (isset($_GET['talent_type']) && !empty($_GET['talent_type'])) {
            switch ($_GET['talent_type']) {
                case '1':
                    $sql .= " and n.is_student = 1";
                    break;
                case '2':
                    $sql .= " and n.is_signed_model = 1";
                    break;
                case '3':
                    $sql .= " and n.is_hot = 1";
                    break;
                case '4':
                    $sql .= " and n.is_judge = 1";
                    break;
                case '5':
                    $sql .= " and n.is_guest = 1";
                    break;
                case '6':
                    $sql .= " and n.is_star = 1";
                    break;
                case '7':
                    $sql .= " and n.is_mentor = 1";
                    break;
                case '8':
                    $sql .= " and n.is_moter = 1";
                    break;
                case '9':
                    $sql .= " and n.is_missworld = 1";
                    break;
                default:
                    break;
            }
        }

        $sql .= " order by t.sort DESC,t.update_time DESC,n.id DESC ";
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $talentinfo = $this->db->my_listinfo($sql, $page, 10);

        $pages = $this->db->pages;
        include $this->admin_tpl('talent_list');
    }
	
    /**
     * 添加资源
     */
    public function add()
    {

        if (isset($_POST['dosubmit'])) {
            $log_content = '在栏目id为'.$_POST['catid'].'下';
            $source_type=isset($_REQUEST['contest_source_type'])?$_REQUEST['contest_source_type']:1;

            if($source_type==1){
                $model=$this->news;
                $log_content .= '<span style="color:green;">添加资讯</span>[资讯id：';
                $information =$model->check($_POST['information']);
            }elseif($source_type==2){
                $model=$this->video;
                $log_content .= '<span style="color:green;">添加视频</span>[视频id：';
                $information =$model->check($_POST['manage']);
            }elseif($source_type==3){
                $model=$this->picture;
                $log_content .= '<span style="color:green;">添加图集</span>[图集id：';
                $information =$model->check($_POST['picture']);
            }

            $info = $model->get_one(array('title'=>$information['title'],'status'=>1,'siteid'=>$this->siteid));
            if (!empty($info)) {
                showmessage(L('title-exist'), HTTP_REFERER);
            }

            $id = $model->edit($information);
            //添加缓存
           // $this->news->redisCache($id);
            if ($id) {
                if ($_POST['contestid']) {
                    $contest_source_model = pc_base::load_model('contest_source_model');
                    $contest_source_model->edit($id, $_POST['contestid'], $source_type);
                }
                $data=array();
                $cateList = array_unique($_POST['othor_catid']);
                foreach ($cateList as $k => $v) {
                    if ($v != $_POST['catid']) {
                        $data[$k]['cate_id']=$v;
                        $data[$k]['source_id']=$id;
                        $data[$k]['source_type']=$source_type;
                        $data[$k]['created_by'] =  $_SESSION['userid'];
                        $data[$k]['updated_by'] =  $_SESSION['userid'];

                        $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
                        $data[$k]['update_time'] = $information['create_time'];
                    }
                }
                $data[count($cateList)]['cate_id']=$_POST['catid'];
                $data[count($cateList)]['source_id']=$id;
                $data[count($cateList)]['source_type']=$source_type;
                $data[count($cateList)]['created_by'] =  $_SESSION['userid'];
                $data[count($cateList)]['updated_by'] =  $_SESSION['userid'];

                $data[count($cateList)]['create_time'] = date("Y-m-d H:i:s",time());
                $data[count($cateList)]['update_time'] = $information['create_time'];
                $res = $this->db->multiInsert($data);

                //记录行为日志
                $log_content .= $id.'；标题：'.$information['title'].']';
                $this->manage_admin_log($log_content);

                if ($res) {
                    showmessage(L('operation_success').L('2s_close'),'blank','','','function set_time() {$("#secondid").html(1);}setTimeout("set_time()", 500);setTimeout("window.close()", 1200);');
                } else {
                    showmessage(L('operation_failure'), HTTP_REFERER);
                }
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            $source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
            $types = $this->type->getSource($source_type);

            if (! isset($_REQUEST['catid']) || empty($_REQUEST['catid'])) {
                showmessage(L('illegal_action'), HTTP_REFERER);
            }
            $catid= (int) $_REQUEST['catid'];
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);

            $action_resources="resources";
            $contest_model = pc_base::load_model('contest_model');
            $categorys = $contest_model->now_contest_select();
            if($source_type==1){
                include $this->admin_tpl('information_add','information');
            }elseif($source_type==2){
                include $this->admin_tpl('manage_add','video');
            }elseif($source_type==3){
                include $this->admin_tpl('picture_add','picture');
            }

        }
    }

    /**
     * 修改图片
     */
    public function editimage ()
    {
        if (!isset($_GET['id']) || empty($_GET['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = intval($_GET['id']);
        if (isset($_POST['dosubmit'])) {
            $res = $this->db->update(array('image'=>$_POST['image']),array('id'=>$id));
            if($res) {
                showmessage(L('operation_success'), HTTP_REFERER, '', 'edit');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $info = $this->db->get_one(array('id'=>$id));
            $show_dialog = $show_validator = true;
            $show_header = '';
            include $this->admin_tpl('editimage');
        }
    }

    /**
     * 修改资源
     */
    public function edit ()
    {
        if (!isset($_GET['id']) || empty($_GET['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = intval($_GET['id']);
        $source_type = intval($_GET['source_type']);
        $catid = intval($_GET['catid']);
        if (isset($_POST['dosubmit'])) {
            if($source_type==1){
                $model=$this->news;
                $log_content .= '<span style="color:blue;">修改资讯</span>[资讯id：';
                $information =$model->check($_POST['information']);
            }elseif($source_type==2){
                $model=$this->video;
                $log_content .= '<span style="color:blue;">修改视频</span>[视频id：';
                $information =$model->check($_POST['manage']);
            }elseif($source_type==3){
                $model=$this->picture;
                $log_content .= '<span style="color:blue;">修改图集</span>[图集id：';
                $information =$model->check($_POST['picture']);
            }
            $info = $model->get_one(array('title'=>$information['title'],'status'=>1,'siteid'=>$this->siteid));
            if (!empty($info)) {
                if ($info['id'] != $id) {
                    showmessage(L('title-exist'), HTTP_REFERER);
                }
            }

            $id = $model->edit($information,$id);
            if ($id) {
                if ($_POST['contestid']) {
                    $contest_source_model = pc_base::load_model('contest_source_model');
                    $contest_source_model->edit($id, $_POST['contestid'], $source_type);
                }

                //记录行为日志
                $log_content .= $id.'；标题：'.$information['title'].']';
                $this->manage_admin_log($log_content);

                showmessage(L('operation_success').L('2s_close'),'blank','','','function set_time() {$("#secondid").html(1);}setTimeout("set_time()", 500);setTimeout("window.close()", 1200);');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            if($source_type==1){
                $model=$this->news;
            }elseif($source_type==2){
                $model=$this->video;
            }elseif($source_type==3){
                $model=$this->picture;
            }
            $info = $model->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
            extract($info);
            $pictureurls = json_decode($pictureurls);
            $show_dialog = $show_validator = true;
            $show_header = '';
            $source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
            $types = $this->type->getSource($source_type,$type_id);

            if (! isset($_REQUEST['catid']) || empty($_REQUEST['catid'])) {
                showmessage(L('illegal_action'), HTTP_REFERER);
            }
            $catid= (int) $_REQUEST['catid'];
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);

            $action_resources="resources";
            $contest_source_model = pc_base::load_model('contest_source_model');
            $contest_model = pc_base::load_model('contest_model');
            $csInfo = $contest_source_model->get_one(array('source_id'=>$id));

            $categorys = $contest_model->now_contest_select($csInfo['contest_id']);
            if($source_type==1){
                include $this->admin_tpl('information_edit');
            }elseif($source_type==2){
                include $this->admin_tpl('manage_edit');
            }elseif($source_type==3){
                include $this->admin_tpl('picture_edit');
            }

        }
    }

    /**
     * 选择资源
     */
    public function bindView(){
        $catid= $_REQUEST['catid'];
        $source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
        $source_array=$this->db->select(array("cate_id"=>$catid,"source_type"=>$source_type),"source_id");
        //获取管理员真实姓名数据
        $admin_model = pc_base::load_model('admin_model');
        $adminInfo = $admin_model->getRealnameInfo();

        $where=" status=1 and  siteid =" . $this->siteid;

        // 搜索框
        $keywords = isset($_GET['keywords']) ? $_GET['keywords'] : '';
        $typeid = isset($_GET['typeid']) ? $_GET['typeid'] : '';
        $image_square = isset($_GET['image_square']) ? $_GET['image_square'] : '';

        if (isset($_GET['search'])) {
            if ($keywords) {
                $where .= " and `title` LIKE '%$keywords%'";
            }
            if ($typeid) {
                $where .= " and type_id = {$typeid}";
            }
            if ($image_square == 1) {
                $where .= " and image_square1 != '' and image_square2 != '' and image_square3 != ''";
            } elseif($image_square == 2){
                $where .= " and (image_square1 = '' or  image_square2 = '' or image_square3 = '')";
            }
        }

        if(!empty($source_array)){
            foreach ($source_array as $k => $v) {
                $idSource[] = intval($v['source_id']);
            }
            $idSource = implode('\',\'', $idSource);
            $where.=" and id not in ('$idSource')";
        }

        $select_categorys = $this->type->getSource($source_type,$typeid);


        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        if($source_type==1){
            $model=$this->news;

        }elseif($source_type==2){
            $model=$this->video;

        }elseif($source_type==3){
            $model=$this->picture;

        }
        $url = "?m=admin&c=resources&a=bindTypeSource&source_type=".$source_type."&catid=".$catid;
        $infos=$model->listinfo($where,'id DESC',$page,10);
        $pages = $model->pages;
        if($source_type==1){
            include $this->admin_tpl('information_select',"information");
        }elseif($source_type==2){
            include $this->admin_tpl('video_select','video');
        }elseif($source_type==3){
            include $this->admin_tpl('picture_select','picture');
        }

    }

    /**
     * 发布资源
     */
    public function ajaxRelease ()
    {
        $cate_id= $_REQUEST['cate_id'];
        $id=$_REQUEST['id'];
        $source=explode(",",$id);
        $data=array();
        $source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
        $msg = '';
        $ids = array();

        foreach ($source as $k => $v) {
            $res = $this->db->get_one(array('source_id'=>$v,'cate_id'=>$cate_id,'source_type'=>$source_type));
            if(empty($res)) {
                $data[$k]['cate_id']=$cate_id;
                $data[$k]['source_id']=$v;
                $data[$k]['source_type']=$source_type;
                $data[$k]['is_top']=0;
                $data[$k]['sort']=0;
                $data[$k]['created_by'] =  $_SESSION['userid'];
                $data[$k]['updated_by'] =  $_SESSION['userid'];

                $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
                $data[$k]['update_time'] = date("Y-m-d H:i:s",time());
            } else {
                $ids[] = $v; 
            }
        }
        if (!empty($ids)) {
            $ids = implode(',',$ids);
            $msg .= '资源id为:' . $ids . '已经绑定该栏目。';
        }
        if (empty($data)) {
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>$msg."发布失败"));die;
        }
        $res=$this->db->multiInsert($data);
        if($res){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>$msg."发布成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>$msg."发布失败"));
        }
    }

    /**
     * 绑定资源
     */
    public function  bindAction(){
        $catid= $_REQUEST['catid'];
        $relation=$_REQUEST['relation'];
        $source=explode("|",$relation);
        //获取栏目信息
        $category_model = pc_base::load_model('category_model');
        $catinfo = $category_model->get_one(array('catid'=>$catid)); 
        $data=array();
        $source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;
        if($source_type == 1){
            $model=$this->news;
        }elseif($source_type == 2){
            $model=$this->video;
        }elseif($source_type == 3){
            $model=$this->picture;
        }

        foreach($source as $k=>$v){

            $data[$k]['cate_id']=$catid;
            $data[$k]['source_id']=$v;
            $data[$k]['source_type']=$source_type;
            $data[$k]['is_top']=0;
            $data[$k]['sort']=0;
            $data[$k]['created_by'] =  $_SESSION['userid'];
            $data[$k]['updated_by'] =  $_SESSION['userid'];
            $info = $model->get_one(array('id'=>$v),'create_time');
            $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
            $data[$k]['update_time'] = $info['create_time'];
        }
        $res=$this->db->multiInsert($data);

        //记录行为日志
        $source_id = implode(',',$source);
        $source_name = $source_type == 1?'资讯':($source_type == 2?'视频':'图集');
        $log_content = '<span style="color:green;">建立绑定关系</span>[栏目id：'.$catid.',栏目名称：'.$catinfo['catname'].'和'.$source_name.'id：('.$source_id.')的绑定]';
        $this->manage_admin_log($log_content);

        if($res){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
        }
    }

    /**
     * ajax一键导入类型下资源
     */
    public function bindTypeSource ()
    {
        $catid= (int) $_REQUEST['catid'];
        $source_type=isset($_REQUEST['source_type'])?(int)$_REQUEST['source_type']:1;
        $type_id = (int) $_REQUEST['type_id'];
        $source_array=$this->db->select(array("cate_id"=>$catid,"source_type"=>$source_type),"source_id");
        $typeInfo = $this->type->get_one(array('id'=>$type_id));
        //获取栏目信息
        $category_model = pc_base::load_model('category_model');
        $catinfo = $category_model->get_one(array('catid'=>$catid));
        //组合条件
        $where = " type_id = ".$type_id;
        if(!empty($source_array)){
            foreach ($source_array as $k => $v) {
                $id[] = intval($v['source_id']);
            }
            $id = implode('\',\'', $id);
            $where.=" and id not in ('$id')";
        }
        if ($typeInfo['parentid'] > 0) {
            if($source_type==1){
                $source_name = '资讯';
                $model=$this->news;
            }elseif($source_type==2){
                $source_name = '视频';
                $model=$this->video;
            }elseif($source_type==3){
                $source_name = '图集';
                $model=$this->picture;
            }
            $info = $model->select($where,'id');
            $data=array();
            $source_id = array();
            foreach($info as $k=>$v){
                $source_id[] = $v['id'];
                $data[$k]['cate_id']=$catid;
                $data[$k]['source_id']=$v['id'];
                $data[$k]['source_type']=$source_type;
                $data[$k]['created_by'] =  $_SESSION['userid'];
                $data[$k]['updated_by'] =  $_SESSION['userid'];
                $infos = $model->get_one(array('id'=>$v['id']),'create_time');
                $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
                $data[$k]['update_time'] = $infos['create_time'];
            }
            if (empty($data)) {
                echo json_encode(array("status"=>true,"code"=>200,"msg"=>"一键导入成功"));die;
            }
            $res=$this->db->multiInsert($data);
            if($res){

                //记录行为日志
                $source_id = implode(',',$source_id);
                $log_content = '<span style="color:green;">建立绑定关系</span>[栏目id：'.$catid.'，栏目名称：'.$catinfo['catname'].'和'.$source_name.'id：('.$source_id.')的绑定]';
                $this->manage_admin_log($log_content);

                echo json_encode(array("status"=>true,"code"=>200,"msg"=>"一键导入成功"));
            }else{
                echo json_encode(array("status"=>false,"code"=>1000,"msg"=>"一键导入失败"));
            }
        } else {
            echo json_encode(array("status"=>false,"code"=>1000,"msg"=>"请选择最底层类型"));
        }
    } 

    /**
     * 删除、批量删除
     */
    public function del()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        $source_id = array();
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $info = $this->db->get_one(array('id'=>$v));
                    $cate_id = $info['cate_id'];
                    $source_name = $info['source_type'];
                    $source_id[] = $info['source_id'];
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        } else {
            $info = $this->db->get_one(array('id'=>$id));
            $cate_id = $info['cate_id'];
            $source_name = $info['source_type'];
            $source_id[] = $info['source_id'];
        }
        $result = $this->db->delete("id in ('$id')");

        //获取栏目信息
        $category_model = pc_base::load_model('category_model');
        $catinfo = $category_model->get_one(array('catid'=>$cate_id));

        //记录行为日志
        $source_id = implode(',',$source_id);
        $source_name = $source_name == 1?'资讯':($source_name == 2?'视频':($source_name == 3?'图集':($source_name == 4?'人才':'课程')));
        $log_content = '<span style="color:red;">解除绑定关系</span>[解除栏目id：'.$cate_id.'，栏目名称：'.$catinfo['catname'].'和'.$source_name.'id：('.$source_id.')的绑定]';
        $this->manage_admin_log($log_content);

        if ($result) {
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * 资源绑定批量删除
     */
    public function deleteAll ()
    {

        if(isset($_GET['dosubmit'])) {
            $id = implode('\',\'', $_POST['ids']);
            $source_id = array();
            foreach ($_POST['ids'] as $v) {
                $info = $this->db->get_one(array('id'=>$v));
                $cate_id = $info['cate_id'];
                $source_name = $info['source_type'];
                $source_id[] = $info['source_id'];
            }

            $result = $this->db->delete("id in ('$id')");
            //获取栏目信息
            $category_model = pc_base::load_model('category_model');
            $catinfo = $category_model->get_one(array('catid'=>$cate_id));

            //记录行为日志
            $source_id = implode(',',$source_id);
            $source_name = $source_name == 1?'资讯':($source_name == 2?'视频':($source_name == 3?'图集':'人才'));
            $log_content = '<span style="color:red;">解除绑定关系</span>[解除栏目id：'.$cate_id.'，栏目名称：'.$catinfo['catname'].'和'.$source_name.'id：('.$source_id.')的绑定]';
            $this->manage_admin_log($log_content);

            if ($result) {
                showmessage(L('operation_success'), HTTP_REFERER);
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            showmessage(L('operation_failure'));
        }
    }
	
    /**
     * 置顶
     */
    public function  top(){

        $result = $this->db->update(array('is_top'=> $_GET['is_top']), array(
            'id' => $_GET['id']
        ));
        
        if($result){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"更新成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"更新失败"));
        }
    }

    /**
     * 排序
     */
    public function listorder() {
        if(isset($_GET['dosubmit'])) {
            foreach($_POST['listorders'] as $id => $listorder) {

                $result = $this->db->update(array('sort'=>$listorder), array(
                    'id' => $id
                ));

            }
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }
}
?>